package org.mogu.tools;

import org.json.JSONException;
import org.json.JSONObject;
import org.mogu.control.model.IMUser;
import org.mogu.db.DBhelper;
import org.mogu.service.InfoSync;
import org.mogu.staticvalue.Convert;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserUtil {
	
	public static void getUser(Context context){
		if(Convert.uname==null||Convert.upwd==null){
			getUserInfo(context);
		}
	}
	
	public static void getUserInfo(Context context){
		synchronized (InfoSync.class) {
	        SQLiteDatabase readdb = DBhelper.getDBHelper(context).getReadableDatabase();
	        Cursor activelist = readdb
					.rawQuery(
							"select username,password from activeuser order by isactive desc",
							null);
	        activelist.moveToFirst();
	        if(!activelist.isAfterLast()){
	        	Convert.uname=activelist.getString(0);
	            Convert.upwd=activelist.getString(1);
	        }
	        activelist.close();
	        readdb.close();
	        }
	}
	
	public static void getImUserByJson(){
		
	}
	
	public static void getJsonbyImUser(JSONObject user,IMUser imuser){
		try {
			user.put("username", imuser.getUname());
			user.put("nickname", imuser.getNicename());
			user.put("birthday", imuser.getBirthday());
			user.put("sheng", imuser.getSheng());
			user.put("city", imuser.getCity());
			user.put("sex", imuser.getSex());
			user.put("header", imuser.getHeader());
			if(imuser.getDesc()!=null){
				user.put("desc",imuser.getDesc() );
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
